const app = getApp();
Page({
  data: {
    xhx_images: app.xhx_images,
    xhx_route: app.xhx_route,
    showComInput:false,//评论框
  },
  onLoad,
  onShow,
  onReachBottom, //页面上拉触底事件的处理函数
  listDataInit,
  getPageList, //获取列表
  comFirst,//一级评论
  comSecond,//二级评论
  sendCom,//评论
  showAll,//查看全部评论
})

function onLoad(options) {
  const that = this;
  const uuid = options && options.uuid || 0;
  that.setData({
    uuid,
  })
  that.listDataInit();
}

function onShow() {
  const that = this;
  that.setData({
    headPortrait: app.userInfo.headPortrait
  })
}

function onReachBottom() {
  const that = this;
  that.getPageList(true);
}

function listDataInit() {
  const that = this;
  that.setData({
    keyword: '', //搜索框输入值
    lists: [], //列表数据
    msg: '请稍等...', //提示信息
    footMsg: "没有更多数据啦！", //底部提示信息
    page: 1, //当前页数
    row: 10, //1页的条数
    isReach: false, //是否全部加载完毕
  })
  that.getPageList();
  return true;
}

/**
 * 获取列表
 * param isReachBottom [Boolean] 是否上拉加载触发
 */
function getPageList(isReachBottom) {
  var that = this;
  var lists = that.data.lists;
  var row = that.data.row;
  var page = that.data.page;
  var isReach = that.data.isReach;
  var uuid = that.data.uuid;
  var footMsg = "";

  if (isReachBottom) {
    if (isReach) return false;
    page++;
  }

  var reqData = {
    pageSize: row,
    pageNum: page,
    contentUuid: uuid, //动态uuid
  }

  app.middleware.requestAjax({
    serAddr: app.serAddrs.commentList,
    data: reqData,
    cancelToast: true,
    success: function (res) {
      console.log(res)
      var tempLists = res.data || [];

      if (row <= tempLists.length) {
        isReach = false;
        footMsg = "上拉加载更多";
      } else {
        isReach = true;
        footMsg = "没有更多数据啦！";
      }
      that.setData({
        msg: "",
        page: page,
        lists: lists.concat(tempLists),
        isReach: isReach,
        footMsg: footMsg,
      })
    },
    fail: function (res) {

      if (isReachBottom) {
        that.setData({
          footMsg: res && res.msg || "加载失败",
        })
      } else {
        that.setData({
          msg: res && res.msg || "加载失败",
        })
      }

    }
  })
}

function comFirst(event){
  const that = this;
  var lists = that.data.lists;
  const index = event.currentTarget.dataset.index;

  that.setData({
    clickTag:{
      callName: lists[index].username,
      index,
      childIndex:null,
    },
    showComInput:true
  })
}

function comSecond(event) {
  const that = this;
  var lists = that.data.lists;
  const index = event.currentTarget.dataset.index;
  const idx = event.currentTarget.dataset.idx;

  that.setData({
    clickTag: {
      callName: lists[index].tCommentList[idx].username,
      index: index,
      childIndex: idx,
    },
    showComInput: true
  })
}

function sendCom(event) {
  const that = this;
  const value = event.detail.value.value.trim();
  const uuid = that.data.uuid;
  var lists = that.data.lists;
  const index = that.data.clickTag.index;
  const childIndex = that.data.clickTag.childIndex;

  var myInfo = app.isNavigateToLogin();
  if (!myInfo) return;

  myInfo = app.isNavigateToBind();
  if (!myInfo) return;

  if(!value) return;

  var reqData = {
    contentUuid: uuid,  //动态uuid
    remark: '',
    comment: value, //评论内容
    userUuid: myInfo.userId, //当前评论发布人uuid
    fatherCommentUuid: lists[index].uuid, //最上级评论uuid

    supperCommentUuid: '0',//上一级评论uuid,不存在则为“0”
    supperUserUuid: '0', //上一级评论发布人uuid
  };

  if (childIndex != null){
    reqData.supperCommentUuid = lists[index].tCommentList[childIndex].uuid;
    reqData.supperUserUuid = lists[index].tCommentList[childIndex].userUuid;
  }

  app.middleware.requestAjax({
    serAddr: app.serAddrs.commentAdd,
    data: reqData,
    success: function (res) {
      const lists = that.data.lists || [];

      if (childIndex != null){
        lists[index].tCommentList.push(res.data)
      }else{
        lists[index].tCommentList.unshift(res.data)
      }
      
      that.setData({
        lists,
        comValue:"",
        showComInput: false
      })

      var prevPage = getCurrentPages()[getCurrentPages().length - 2];

      if (prevPage.route == "pages/home/details/details") {
        prevPage.setData({
          commentCount: (prevPage.data.commentCount || 0) + 1
        })
      }
    },
    complete: function (res) {
      app.middleware.showToast(res && res.msg || "操作失败");
    }
  })
}

function showAll(event){
  const that = this;
  var lists = that.data.lists;
  const index = event.currentTarget.dataset.index;
  lists[index].isShowAll=true;
  that.setData({
    lists,
  })
}